package com.company;

import java.util.HashMap;
import java.util.Map;

public class Test07统计字符 {
    public static void main(String[] args) {
        char str[]= {'s', 'a', '1'};
        zifu(str);
        String ch = "dddd sda dasd2121 !";
        Map<Integer,Integer> m = countChar(ch);
        for(int i=1;i<5;i++){
            System.out.println(m.get(i));
        }
    }

    /**
     * 统计字符
     * @param str
     */
    public static void zifu(char str[]){
        int count1 = 0,count2=0,count3=0,count4=0;
        for(int i=0;i<str.length;i++){
            if(str[i]>='a'&&str[i]<='z'||str[i]>='A'&&str[i]<='Z'){
                count1++;
            }else if(str[i]>=' '){
                count2++;
            }else if(str[i]>='0'&&str[i]<='9'){
                count3++;
            }else{
                count4++;
            }
        }
        System.out.println("字母"+count1);
        System.out.println("空格"+count2);
        System.out.println("数字"+count3);
        System.out.println("其他字符"+count4);
    }
    public static Map<Integer,Integer> countChar(String str){
        Map<Integer,Integer> m = new HashMap<>();
        for (int i=1;i<5;i++){
            m.put(i,0);
        }
        char ch [] = str.toCharArray();
        for(int i =0;i<ch.length;i++){
            if(ch[i]>='a'&&ch[i]<='z'||ch[i]>='A'&&ch[i]<='Z'){
                m.put(1,m.get(1)+1);
            }else if(ch[i]>=' '){
                m.put(2,m.get(2)+1);
            }else if(ch[i]>='0'&&ch[i]<='9'){
                m.put(3,m.get(3)+1);
            }else{
                m.put(4,m.get(4)+1);
            }
        }
        return m;

    }
}
